﻿using System;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;

namespace HMIControl
{
    [Startable]
    public class Fan : HMIControlBase
    {
        static Fan()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(Fan), new FrameworkPropertyMetadata(typeof(Fan)));
        }

        public override string[] GetActions()
        {
            return new string[] { TagActions.VISIBLE, TagActions.CAPTION, TagActions.RUN, TagActions.ALARM, TagActions.DEVICENAME };
        }

        public override LinkPosition[] GetLinkPositions()
        {
            return new LinkPosition[1]
                {
                    new  LinkPosition(new Point(0.75,0),ConnectOrientation.Top),
                };
        }

        public override Action SetTagReader(string key, Delegate tagChanged)
        {
            switch (key)
            {
                case TagActions.RUN:
                    var _funcInRun = tagChanged as Func<bool>;
                    if (_funcInRun != null)
                    {
                        return delegate { VisualStateManager.GoToState(this, _funcInRun() ? "Running" : "NotRunning", true); };
                    }
                    else return null;
                case TagActions.ALARM:
                    var _funcAlarm = tagChanged as Func<bool>;
                    if (_funcAlarm != null)
                    {
                        return delegate { VisualStateManager.GoToState(this, _funcAlarm() ? "Alarm" : "Unalarm", true); };
                    }
                    else return null;
            }
            return base.SetTagReader(key, tagChanged);
        }
    }




    [Startable]
    public class FanPin : HMIControlBase
    {
        static FanPin()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(FanPin), new FrameworkPropertyMetadata(typeof(FanPin)));
        }

        public override string[] GetActions()
        {
            return new string[] { TagActions.VISIBLE };
        }

        public override LinkPosition[] GetLinkPositions()
        {
            return new LinkPosition[2]
                {
                    new  LinkPosition(new Point(0.1,0),ConnectOrientation.Top),
                    //new  LinkPosition(new Point(0.3,0),ConnectOrientation.Top),
                    //new  LinkPosition(new Point(0.5,0),ConnectOrientation.Top),
                    //new  LinkPosition(new Point(0.7,0),ConnectOrientation.Top),
                    new  LinkPosition(new Point(0.9,0),ConnectOrientation.Top),

                };
        }

        public FanPin()
        {
            this.Loaded += FanPin_Loaded;
        }
        ControlAdorner kdaad;
        private void FanPin_Loaded(object sender, RoutedEventArgs e)
        {
            var adolay = AdornerLayer.GetAdornerLayer(this);
            var ados = adolay.GetAdorners(this);

            if (ados == null)
            {
                kdaad = new ControlAdorner(this);
                /*
                foreach (var pin in kdaad.Children)
                {
                    pin.MouseLeftButtonDown += new MouseButtonEventHandler(Pin_MouseLeftButtonDown);//按下左键选中hitPin，开始拖动
                    pin.MouseMove += new MouseEventHandler(Pin_MouseMove);//移动鼠标，开始找寻目标连接节点
                }
                */

                if (DesignerProperties.GetIsInDesignMode(this) == false)
                    adolay.Add(kdaad);

            }

            //ados = adolay.GetAdorners(this);
            //var ado = ados.FirstOrDefault() as PinAdorner;

   
            UpdateState();



        }


        /*

                LinkPin hitPin;
                /// <summary>
                /// 拖动
                /// </summary>
                bool isdrag = false;

                private void Pin_MouseLeftButtonDown(object s, MouseButtonEventArgs e)
                {
                    hitPin = s as LinkPin; isdrag = true; e.Handled = true;
                }

                void Pin_MouseMove(object s, MouseEventArgs e)
                {
                    if (e.LeftButton != MouseButtonState.Pressed) isdrag = false;

                    if (isdrag && hitPin != null)
                    {
                        //在移动时创建一个临时的控件
                        PinAdorner pinAdorner = new PinAdorner(this.Parent as Panel, hitPin);
                        pinAdorner.MouseUp += new MouseButtonEventHandler(pinAdorner_MouseUp);

                        (this.Parent as Panel).Children.Add(pinAdorner);
                        e.Handled = true;
                    }
                }
                /// <summary>
                /// 方向绑定Map
                /// </summary>
                static readonly Dictionary<ConnectOrientation, string> omap = new Dictionary<ConnectOrientation, string>
                { { ConnectOrientation.Left, "LeftPin" }, { ConnectOrientation.Right, "RightPin" },
                { ConnectOrientation.Top, "TopPin" }, { ConnectOrientation.Bottom, "BottomPin" },{ ConnectOrientation.None, "" } };
                void pinAdorner_MouseUp(object sender, MouseButtonEventArgs e)
                {
                    var pinAdorner = sender as PinAdorner;
                    var _panel = (this.Parent as Panel);
                    if (pinAdorner.HitPin != null)
                    {
                        LinkableControl targetObject = pinAdorner.HitLinkableControl;
                        LinkPin lp1 = pinAdorner.SourcePin;
                        LinkPin lp2 = pinAdorner.HitPin;

                        ConnectInfo srcinfo = lp1.GetInfo();
                        ConnectInfo taginfo = lp2.GetInfo();

                        LinkLine line = new LinkLine(srcinfo, taginfo);

                        Binding bi1 = new Binding(omap[srcinfo.Orient]);
                        bi1.ElementName = this.Name;
                        line.SetBinding(LinkLine.OriginInfoProperty, bi1);
                        Binding bi2 = new Binding(omap[taginfo.Orient]);
                        bi2.ElementName = targetObject.Name;
                        line.SetBinding(LinkLine.TargetInfoProperty, bi2);

                        //var CanvasModelItem = AdornerProperties.GetModel(adorner).Parent;
                        //ModelItemCollection myControls = CanvasModelItem.Properties["Children"].Collection;
                        //ModelItem myLinkLine = myControls.Add(line);

                        _panel.Children.Add(line);
                    }
                    if (pinAdorner.HitLinkableControl != null)
                    {
                        pinAdorner.HitLinkableControl.IsLinkDragOver = false;
                    }

                    hitPin = null;
                    if (kdaad.IsMouseCaptured) 
                        kdaad.ReleaseMouseCapture();
                    _panel.Children.Remove(pinAdorner);
                }

                */


        public override Action SetTagReader(string key, Delegate tagChanged)
        {
            switch (key)
            {
                case TagActions.RUN:
                    var _funcInRun = tagChanged as Func<bool>;
                    if (_funcInRun != null)
                    {
                        return delegate { VisualStateManager.GoToState(this, _funcInRun() ? "Running" : "NotRunning", true); };
                    }
                    else return null;
                case TagActions.ALARM:
                    var _funcAlarm = tagChanged as Func<bool>;
                    if (_funcAlarm != null)
                    {
                        return delegate { VisualStateManager.GoToState(this, _funcAlarm() ? "Alarm" : "Unalarm", true); };
                    }
                    else return null;
            }
            return base.SetTagReader(key, tagChanged);
        }

        protected override void UpdateState()
        {
            VisualStateManager.GoToState(this, Alarm ? "Alarm" : "Unalarm", true);
            VisualStateManager.GoToState(this, Running ? "Running" : "NotRunning", true);
        }

        public static readonly DependencyProperty RunningProperty =
       DependencyProperty.Register("Running", typeof(bool), typeof(FanPin),
       new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender,
       new PropertyChangedCallback(OnValueChanged)));



        [Category("HMI")]
        public bool Running
        {
            get
            { return (bool)GetValue(RunningProperty); }
            set
            { SetValue(RunningProperty, value); }
        }


    }



}
